const app = getApp()
const {
  wxPost, imgHost,
  wxGet,
  commonShare,
  getJwt
} = require('../../utils/common.js')
Page({
  data: {
    imgHost,
    userInfo: {},
    hasWxAuth: false, //是否授权
    hasUserInfo: false,
    waitLoading: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    cardCur: 0,
    videoCur:0,
    videoPlay:false,
    videoUrl:''
  },
  onReady: function() {
    //获得menu组件
    this.menu = this.selectComponent("#menu");
    this.videoswiper = this.selectComponent("#videoSwiper")
    this.videoContext = wx.createVideoContext('myVideo')
    wxGet('/common/pageInfo',null,({data})=>{
      if (data && data['errorCode'] === 0){
        const { imgSetting, linkSetting } = data
        let feedbackList = imgSetting && imgSetting.filter(o => o.type === 1)
        let videoList = imgSetting && imgSetting.filter(o => o.type === 2)
        let classcialImgList = imgSetting && imgSetting.filter(o => o.type === 3)
        this.setData({ feedbackList, videoList, classcialImgList, linkSetting: linkSetting||{}})
      }
    })
  },


  offVideo:function(){
    this.setData({
      videoUrl:'',
      videoPlay:false
    })
    this.videoContext.stop()
  },
  onLoad: function() {
    const that = this

    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            lang: "zh_CN",
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              app.globalData.userInfo = res.userInfo
              app.globalData.hasWxAuth = true
              wx.hideLoading()
              that.checkLoginStatus()
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        } else {
          that.setData({
            waitLoading: false
          })
          that.checkLoginStatus()
        }
      },
      fail: function() {
        that.checkLoginStatus()
      }
    })
  },
  checkLoginStatus: function() { //checkLoginStatus(session)-->login(如果授权登录login刷新个人信息,如果未授权页面打开授权,确定授权login)
    const that = this
    app.globalData.userId = wx.getStorageSync("userId")
    if (app.globalData.userInfo.userId) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }
    wx.checkSession({
      success: function() {
        wx.hideLoading()
        console.info('session working ....')
        if (!app.globalData.userId || !getJwt()) {
          that.loginServer()
        }
      },
      fail: function(options) { //重新登录
        console.info('session time out ....')

        that.loginServer()
      }
    })
  },
  loginServer() {
    app.globalData.userId = ''
    const that = this
    app.appLogin().then(function() {
      if (app.globalData.userInfo) {
        that.setData({
          userInfo: app.globalData.userInfo
        })
        wx.hideLoading()
      }
    })
  },
  getUserInfo: function(e) {
    if (e && e.detail.userInfo) {
      if (!app.globalData.hasWxAuth){//for update userinfo 
        app.globalData.hasWxAuth = true
        app.globalData.userInfo = e.detail.userInfo
        this.loginServer()

      }
    }
  },
  linkTap(e){
    const that=this
    const { type, url } = e.currentTarget.dataset
    if (url && type) {
      if (type === 'article') {
        app.globalData.src = url
        wx.navigateTo({
          url: '../foward',
        })
      } else if (type === 'video') {
        wxGet(`/common/getVideoUrl/${url}`,null,({data})=>{
          if(data && data.errorCode===0){
            that.setData({
              videoUrl: data['url'],
              videoPlay: true
            })
          }
        })
        
      }
    }
  },
  swiperItemChange(e) {
    const { cur } = e.currentTarget.dataset
    const current = e.detail.current
    this.setData({
      [cur]: current
    })
  },
  viewImg(e){
    const { src} = e.currentTarget.dataset
    if(src){
      wx.previewImage({
        urls: [src]
      })
    }
  }
})